package com.bw.gmall.realtime.common.util;

import java.time.LocalDate;
import java.time.Period;

public class AgeUtil {

    public static String[] getAgeType(int age){
        //转换为年龄
        String  yearBaseType ="" ;
        String  yearBaseName ="" ;
        //判断
        if(age >= 70 && age < 80){
            yearBaseType = "L80";
            yearBaseName = "40后";
        }else if (age >= 60 && age < 70){
            yearBaseType = "L70";
            yearBaseName = "50后";
        }else if (age >= 50 && age < 60){
            yearBaseType = "L60";
            yearBaseName = "60后";
        }else if (age >=   40 && age < 50){
            yearBaseType = "L50";
            yearBaseName = "70后";
        }else if (age >= 30 && age < 40){
            yearBaseType = "L40";
            yearBaseName = "80后";
        }else if (age >= 20 && age < 30){
            yearBaseType = "L30";
            yearBaseName = "90后";
        }else if (age >= 10 && age < 20){
            yearBaseType = "L20";
            yearBaseName = "00后";
        }else if (age < 10 ){
            yearBaseType = "L10";
            yearBaseName = "10后";
        }
        String[] years = new String[2];
        years[0] = yearBaseType;
        years[1] = yearBaseName;
        return years;
    }

    /**
     * 根据生日算年龄
     * @param birthday
     * @return
     */
    public static int getAgeByBirthday(String birthday) {
        if (birthday.contains(" ")) {
            birthday = birthday.split(" ")[0];
        }
        LocalDate currentDate = LocalDate.now();
        // 将字符串转换为LocalDate类型
        LocalDate birthDate = LocalDate.parse(birthday);
        // 计算年龄
        Period period = Period.between(birthDate, currentDate);
        return period.getYears();
    }
}
